package com.xc.videosample

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.VideoView
import androidx.databinding.DataBindingUtil
import com.feo.kpler.HttpMediaCacheServer
import com.feo.kpler.videocache.CacheListener
import com.feo.kpler.videocache.KLog
import com.xc.videosample.databinding.ActivityMainBinding
import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL

class MainActivity : AppCompatActivity() {
    lateinit var dataBinding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        dataBinding =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        dataBinding.tvAudio.setOnClickListener {
            AudioActivity.start(this)
        }
        val videoUri =
            "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=2178&editionType=default&source=aliyun"
        KLog.init(true, "YWLog")
        dataBinding.tvExoVideo.setOnClickListener {
//            ExoActivity.start(this)

            HttpMediaCacheServer.getProxy()?.let {
                it.registerCacheListener({ cacheFile, url, percentsAvailable ->
                    Log.i("YWLog", " cache Finish")
                }, videoUri)
                val proxyUrl = it.getProxyUrl(videoUri)
                Log.i("YWLog", "proxyUrl: ${proxyUrl}")

                Thread(object : Runnable {
                    override fun run() {

                        val url = URL(proxyUrl)
                        val connect = url.openConnection()
                        connect.connect()
                        if (connect is HttpURLConnection) {
                            val responseCode = connect.responseCode
                            if (responseCode == 200) {
                                Log.i("YWLog", " cache connect ${Thread.currentThread().name}")
                            }
                        }
                        Log.i("YWLog", " cache connect ${Thread.currentThread().name}")
                    }
                }, "Thread_proxy").start()
            }
        }

        dataBinding.tvFragmentPlayer.setOnClickListener {
            ViewPage2Activity.start(this)
        }


        dataBinding.tvVideo.setOnClickListener {
//            VideoPlayerActivity.start(this)
            val itemView = layoutInflater.inflate(R.layout.item_fake_view, null)
            dataBinding.flContainer.addView(itemView)
        }


    }
}